package com.oauth.application.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;

public class UnSubscribeApplicationButton extends PushButton {
	public UnSubscribeApplicationButton(final ApplicationClientBean pApplicationClientBean,
			final ApplicationRemoteServiceAsync pApplicationService) {

		super(new Image("/common/images/icon_close_blue.gif"));
		this.setStylePrimaryName("removeButton");
		final Long applicationId = pApplicationClientBean.getId();
		addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent pEvent) {
				boolean confirm = Window.confirm("Are you sure to remove this application?");
				if (confirm)
					pApplicationService.unSubscribeFromApplication(applicationId,
							new OAuthAsnyCallBack<String>() {
								public void onSuccess(String result) {
									if (result.equals("success")) {
										Window.Location.assign("/browse.html");
									} else {
										Window
												.alert("Unable to remove application.Try again after some time");
									}
								}
							});
			}
		});
	}
}
